<!DOCTYPE html>
<html class="x-admin-sm">
    <head>
        <meta charset="UTF-8">
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8" />
        <link rel="stylesheet" href="/css/font.css">
        <link rel="stylesheet" href="/css/xadmin.css">
        <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
        <script src="/lib/layui/layui.js" charset="utf-8"></script>
        <script type="text/javascript" src="/js/xadmin.js"></script>
        <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
        <!--[if lt IE 9]>
          <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
          <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
    </head>
    <body>
        <div class="x-nav">
            <span class="layui-breadcrumb">
                <a href="">
                    {php}echo config('lang.common.home_tab'){/php}
                </a>
                <a href="">
                    {php}echo config('lang.common.system_tab'){/php}
                </a>
                <a>
                    <cite>
                        {php}echo config('lang.menu.tab'){/php}
                    </cite>
                </a>
            </span>
            <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
                <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
            </a>
        </div>
        <div class="layui-fluid">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="layui-card">
                        <div class="layui-card-body ">
                            <form class="layui-form layui-col-space5">
                                <div class="layui-input-inline layui-show-xs-block">
                                    <input name="__token__" type="hidden" class="layui-input" value="{:token()}">
                                    <input class="layui-input" placeholder="{php}echo config('lang.menu.menu_name'){/php}" name="menu_name">
                                </div>
                                <div class="layui-input-inline layui-show-xs-block">
                                    <input class="layui-input" placeholder="{php}echo config('lang.menu.menu_icon'){/php}" name="menu_icon">
                                </div>
                                <div class="layui-input-inline layui-show-xs-block">
                                    <button class="layui-btn"  lay-submit="" lay-filter="add">
                                        <i class="layui-icon"></i>
                                        {php}echo config('lang.common.add_button'){/php}
                                    </button>
                                </div>
                            </form>
                        </div>
                        <div class="layui-card-body ">
                            <table class="layui-table layui-form">
                              <thead>
                                <tr>
                                  <th width="70">
                                      {php}echo config('lang.common.id'){/php}
                                  </th>
                                  <th>
                                      {php}echo config('lang.menu.menu_name'){/php}
                                  </th>
                                    <th>
                                        {php}echo config('lang.menu.menu_icon'){/php}
                                    </th>
                                  <th>
                                      {php}echo config('lang.common.url'){/php}
                                  </th>
                                  <th width="250">
                                      {php}echo config('lang.common.operating'){/php}
                                  </th>
                              </thead>
                              <tbody class="x-cate">
                              {php}$adminMenu = \app\controller\AdminMenu::getAllMenuData(new \app\model\AdminMenu());{/php}
                              {foreach $adminMenu as $v}
                                <tr cate-id='{$v.id}' fid='{$v.top_id}' >
                                  <td>
                                      {$v.id}
                                  </td>
                                  <td>
                                      {switch $v['top_state'] }
                                        {case 1}
                                        <i class="layui-icon x-show" status='true'>&#xe623;</i>
                                        {/case}
                                        {default /}
                                        ├
                                      {/switch}
                                      {$v.menu_name}
                                  </td>
                                    <td>
                                        {$v.menu_icon | default="二级菜单无 ICON"}
                                    </td>
                                    <td>
                                        {$v.menu_url | default="顶级菜单无 URL"}
                                    </td>
                                  <td class="td-manage">
                                      {switch $v['top_state'] }
                                      {case 1}
                                        <button class="layui-btn layui-btn layui-btn-xs"  onclick="xadmin.open('{php}echo config('lang.common.edit'){/php}','/editTopMenu/{$v.id}', 500, 230)" >
                                          <i class="layui-icon">&#xe642;</i>{php}echo config('lang.common.edit'){/php}</button>
                                        <button class="layui-btn layui-btn-warm layui-btn-xs"  onclick="xadmin.open('{php}echo config('lang.menu.add_children'){/php}','/addSonMenu/{$v.id}', 500, 230)" >
                                            <i class="layui-icon">&#xe642;</i>{php}echo config('lang.menu.add_children'){/php}</button>
                                      {/case}
                                      {default /}
                                      <button class="layui-btn layui-btn layui-btn-xs"  onclick="xadmin.open('{php}echo config('lang.common.edit'){/php}','/editSonMenu/{$v.id}', 500, 230)" >
                                          <i class="layui-icon">&#xe642;</i>{php}echo config('lang.common.edit'){/php}</button>
                                      {/switch}
                                    <button class="layui-btn-danger layui-btn layui-btn-xs"  onclick="member_del(this,'{$v.id}')" href="javascript:;" >
                                        <i class="layui-icon">&#xe640;</i>{php}echo config('lang.common.delete'){/php}</button>
                                  </td>
                                </tr>
                              {/foreach}
                              </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <script>
            layui.use(['form'], function(){
                var form = layui.form;
                //监听提交
                form.on('submit(add)', function(data){
                    getAdminToken();
                    $.ajax({
                        type: "POST",
                        url: "/addTopMenu",
                        data: data.field,
                        dataType: "json",
                        success: function(data){
                            switch(data) {
                                case 200:
                                    layer.msg('操作成功！');
                                    location.reload();
                                    break;
                                default:
                                    layer.msg('未知错误！');
                            }
                        }
                    });
                    return false;
                });
            });

           /*用户-删除*/
          function member_del(obj,id){
              layer.confirm('确认要删除吗？',function(index){
                  $.ajax({
                      type: "POST",
                      url: "/deleteMenu",
                      data: {
                          id:id
                      },
                      dataType: "json",
                      success: function(data){
                          switch(data) {
                              case 200:
                                  $(obj).parents("tr").remove();
                                  layer.msg('已删除!',{icon:1,time:1000});
                                  break;
                              default:
                                  layer.msg('未知错误！');
                          }
                      }
                  });
              });
          }

          // 分类展开收起的分类的逻辑
          $(function(){
            $("tbody.x-cate tr[fid!='0']").hide();
            // 栏目多级显示效果
            $('.x-show').click(function () {
                if($(this).attr('status')=='true'){
                    $(this).html('&#xe625;'); 
                    $(this).attr('status','false');
                    cateId = $(this).parents('tr').attr('cate-id');
                    $("tbody tr[fid="+cateId+"]").show();
               }else{
                    cateIds = [];
                    $(this).html('&#xe623;');
                    $(this).attr('status','true');
                    cateId = $(this).parents('tr').attr('cate-id');
                    getCateId(cateId);
                    for (var i in cateIds) {
                        $("tbody tr[cate-id="+cateIds[i]+"]").hide().find('.x-show').html('&#xe623;').attr('status','true');
                    }
               }
            })
          });

          var cateIds = [];
          function getCateId(cateId) {
              $("tbody tr[fid="+cateId+"]").each(function(index, el) {
                  id = $(el).attr('cate-id');
                  cateIds.push(id);
                  getCateId(id);
              });
          }
   
        </script>
    </body>
</html>
